package com.whz.gateway.configure.access.rules.white;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.ServerWebExchange;
import com.whz.gateway.common.constants.GatewayConstant;
import com.whz.gateway.common.util.PathMatcherUtils;
import com.whz.gateway.limit.IWhiteRule;

import java.util.Arrays;

/**
 * 白名单需要慎重,命中白名单的请求,不会认证和权限控制,一般是静态资源访问
 *
 * @author whz
 */
@Configuration
public class WhiteRulesConfiguration {

    @Bean
    public IWhiteRule staticResourceWhiteRule() {

        return new IWhiteRule() {
            @Override
            public String getName() {
                return "DefaultWhiteRule";
            }

            @Override
            public boolean test(ServerWebExchange exchange) {
                return Arrays.stream(GatewayConstant.WHITE_LIST_PATHS).anyMatch(path -> PathMatcherUtils.match(path, exchange.getRequest().getPath().value()));
            }
        };
    }
}